package com.zhijian.medical.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.zhijian.medical.config.HstV2Properties;
import com.zhijian.medical.entity.dto.*;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @Description: TODO
 * @Author: hcg
 * @Date: 2024/11/22
 * @Version: 1.0
 */
@Slf4j
public class HstMeetingV2Util {

    /**
     * 超时时间，单位毫秒，3秒
     */
    static int TIME_OUT = 1000 * 3;

    /**
     * URL前缀
     */
    static String URL_FRONT = "https://5uw.haoshitong.com:28443/%s";

    /**
     * 创建会议
     *
     * @param requestDto
     * @return
     */
    public static HstMeetingV2CreateResponseDto createMeeting(String token, HstMeetingV2CreateRequestDto requestDto) {
//        String password = saveOrUpdateVo.getPassword();
//        if (StringUtils.isNotBlank(password)) {
//            //设置了密码，模式就是会议室密码验证
//            saveOrUpdateVo.setVerifyMode(2 + "");
//        } else {
//            //否则就是账户密码登录
//            saveOrUpdateVo.setVerifyMode(1 + "");
//        }
        requestDto.setMeetingMinutes(true);
        requestDto.setTerminalReInvite(true);
        try {
            HstV2Properties hstV2Properties = SpringUtil.getBean(HstV2Properties.class);
            //链式构建请求
            HttpResponse httpResponse = HttpRequest.post(String.format(SpringUtil.getBean(HstV2Properties.class).getApiStartUrl(), "api/v1/room/addRoomInfo"))
                    .header("Content-Type", "application/json")
                    .header("Authorization", token)
                    //超时，毫秒
                    .timeout(TIME_OUT)
                    .body(JsonUtil.toString(requestDto))
                    .execute();
            String responseBody = httpResponse.body();
            log.info("the response of createMeeting is {}", responseBody);
            try {
                HstMeetingV2CreateResponseDto hstMeetingV2CommonResult = JsonUtil.toBean(responseBody, HstMeetingV2CreateResponseDto.class);
                return hstMeetingV2CommonResult;
            } catch (Exception e) {
                log.error("the response body of device subscribeMessage is not legal...", e.getMessage());
                e.printStackTrace();
                return null;
            }
        } catch (Exception e) {
            log.error("the request of device subscribeMessage.send cause error...", e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 修改会议室为预约会议室
     *
     * @param token
     * @param requestDto
     * @return
     */
    public static HstMeetingV2CommonResult reservation(String token, HstMeetingV2ReservationRequestDto requestDto) {

        try {
            //链式构建请求
            HttpResponse httpResponse = HttpRequest.post(String.format(SpringUtil.getBean(HstV2Properties.class).getApiStartUrl(), "api/v1/room/reservation"))
                    .header("Authorization", token)
                    .header("Content-Type", "application/json")
                    //超时，毫秒
                    .timeout(TIME_OUT)
                    .body(JsonUtil.toString(requestDto))
                    .execute();
            String responseBody = httpResponse.body();
            log.info("the response of reservation is {}", responseBody);
            try {
                HstMeetingV2CommonResult hstMeetingV2CommonResult = JsonUtil.toBean(responseBody, HstMeetingV2CommonResult.class);
                return hstMeetingV2CommonResult;
            } catch (Exception e) {
                log.error("the response body of device subscribeMessage is not legal...", e.getMessage());
                e.printStackTrace();
                return null;
            }
        } catch (Exception e) {
            log.error("the request of device subscribeMessage.send cause error...", e.getMessage());
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 查询
     *
     * @param token
     * @param requestDto
     * @return
     */
    public static HstMeetingV2MeetingResponseDto roomList(String token, HstMeetingV2SearchMeetingDto requestDto) {
        requestDto.setSearchType("0");
        try {
            //链式构建请求
            HttpResponse httpResponse = HttpRequest.post(String.format(SpringUtil.getBean(HstV2Properties.class).getApiStartUrl(), "api/v1/room/roomList"))
                    .header("Authorization", token)
                    .header("Content-Type", "application/json")
                    //超时，毫秒
                    .timeout(TIME_OUT)
                    .body(JsonUtil.toString(requestDto))
                    .execute();
            String responseBody = httpResponse.body();
            log.info("the response of roomList is {}", responseBody);
            try {
                HstMeetingV2MeetingResponseDto hstMeetingV2CommonResult = JsonUtil.toBean(responseBody, HstMeetingV2MeetingResponseDto.class);
                return hstMeetingV2CommonResult;
            } catch (Exception e) {
                log.error("the response body of roomList is not legal...", e.getMessage());
                e.printStackTrace();
                return null;
            }
        } catch (Exception e) {
            log.error("the request of roomList cause error...", e.getMessage());
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 获取签名
     *
     * @param secret
     * @param key
     * @return
     */
    public static String getSign(String secret, String key) {
        long timestamp = System.currentTimeMillis();


        String header = Base64.getEncoder().encodeToString("{\"alg\": \"SHA256\"}".getBytes(StandardCharsets.UTF_8));

        String loadParam = String.format("{\"key\":\"%s\",\"timestamp\":%d}", key, timestamp);

        String load = Base64.getEncoder().encodeToString((loadParam)
                .getBytes(StandardCharsets.UTF_8));

        // SHA256算法加密
        String signature = SHAUtil.SHA256(header + load + secret);

        return header + "." + load + "." + signature;
    }
}
